package com.pengcheng.hnweb.controller;

import com.pengcheng.hnweb.service.DbService;
import com.pengcheng.hnweb.util.Common;
import com.pengcheng.hnweb.util.ControllerUtil;
import com.pengcheng.hnweb.util.MenuUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class SiteController {
    @Autowired
    private Common common;
    @Autowired
    private MenuUtil menuUtil;
    @Autowired
    private DbService dbService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private ControllerUtil controllerUtil;
    @RequestMapping("/site/{action}")
    public String list_user(HttpServletRequest request, Model model, @PathVariable("action") String action)
    {
        model=controllerUtil.pre(request,model);
        Map<String,String> params=new HashMap<>();
        Map<String,String[]> map=request.getParameterMap();
        for(String key:map.keySet())
        {
            params.put(key,common.join(map.get(key)));
        }
        model.addAttribute("param",params);
        return "site/"+action;
    }


    private Model pre(HttpServletRequest request,Model model)
    {
        model.addAttribute("url",request.getRequestURI());
        //获取menu
        String sql="select * from hnww_menu where deleted=0 and pid=0 order by fsort asc";
        List<Map<String,Object>> rows_menu=jdbcTemplate.queryForList(sql);
        rows_menu=dbService.get_menu_detail(rows_menu);
        model.addAttribute("rows_menu",rows_menu);
        Object id=request.getSession().getAttribute("user_id");
        model.addAttribute("user_id",id);
        sql="select * from hnww_menu where deleted=0";
        List<Map<String,Object>> rows_menu2=jdbcTemplate.queryForList(sql);
        Map<String,Object> row_menu=menuUtil.get_row_menu(menuUtil.get_url(request),rows_menu2);
        if(row_menu!=null)
        {
            model.addAttribute("row_menu",row_menu);
            List<Map<String,Object>> list_menu=menuUtil.get_menu_path(row_menu,rows_menu2);
            model.addAttribute("list_menu",list_menu);
        }
        return model;
    }
}
